﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Net;
using System.Net.Mail;
using System.Web.Configuration;

/// <summary>
/// Summary description for DBClass
/// </summary>
public static class DBClass
{
	public static SqlConnection CreateConnection()
	{
        string strConn = WebConfigurationManager.ConnectionStrings["DB_BookStoreConnectionString"].ToString();
//	    SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["conn"]);
        SqlConnection con = new SqlConnection(strConn);
        if(con.State == ConnectionState.Open) con.Close();
        con.Open();
	    return con;
	}

    public static DataTable SelectData(string strSQL)
    {
        SqlConnection con = CreateConnection();
        SqlDataAdapter da = new SqlDataAdapter(strSQL, con);
        DataTable dt = new DataTable();
        da.Fill(dt);
        con.Close();
        return dt;
    }

    public static DataTable SelectData(string strSQL, params SqlParameter[] parameters)
    {
        SqlConnection con = CreateConnection();
        SqlCommand cmd = new SqlCommand(strSQL, con);
        cmd.Parameters.AddRange(parameters);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        con.Close();
        return dt;
    }

    public static int Execute(string strSQL)
    {
        SqlConnection con = CreateConnection();;
        try
        {
            SqlCommand cmd = new SqlCommand(strSQL, con);
            return cmd.ExecuteNonQuery();
        }
        catch (Exception)
        {
            return -1;
        }
        finally
        {
            con.Close();
        }
    }

    public static int Execute(string strSQL, params SqlParameter[] parameters)
    {
        try
        {
            SqlConnection con = CreateConnection();
            SqlCommand cmd = new SqlCommand(strSQL, con);
            cmd.Parameters.AddRange(parameters);
            int nor = cmd.ExecuteNonQuery();
            con.Close();
            return nor;
        }
        catch (Exception)
        {
            return -1;
        }
    }

    public static bool Login(string username, string password)
    {
        string strSelect = "SELECT username, password FROM users WHERE username = \'" + username +
                           " \' AND password = \'" + password + " \'";
        DataTable dt = SelectData(strSelect);
        return dt.Rows.Count > 0 ? true : false;
    }

    public static bool Register(string username, string password, string email, string fullName, bool gender, string address, string phoneNumber)
    {
        return false;
    }

    public static string CreateRandomString(int length)
    {
        string strRandom = "";
        
        Random autoRand = new Random();
        for(int i = 0; i < length; ++i)
        {
            strRandom += Convert.ToInt32(autoRand.Next(0, 9));
        }
        return strRandom;
    }

    public static bool SendMailByGmail(string to, string strSubject, string strContent)
    {
        MailMessage mail = new MailMessage();
        mail.To.Add(to);
        mail.Subject = strSubject;
        mail.IsBodyHtml = true;
        mail.Body = strContent;
        mail.From = new MailAddress("tranthanhphong91@gmail.com");
        try
        {
            SmtpClient client = new SmtpClient();
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.EnableSsl = true;
            client.Host = "smtp.gmail.com";
            client.Port = 587;

            NetworkCredential credentials = new NetworkCredential("tranthanhphong91@gmail.com", "08110226");
            client.UseDefaultCredentials = false;
            client.Credentials = credentials;
            client.Send(mail); 
            return true;
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error: " + ex.ToString());
            return false;
        }
    }
}
